﻿using System.Collections.ObjectModel;
using System.Windows;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Silverlight.Models;
using Microsoft.Practices.Prism.Commands;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.ViewModels
{
    public class ProductExportViewModel:ViewModelBase
    {
        public ProductExportViewModel()
        {
            Model = new ProductExportModel();
            ((ProductExportModel)Model).GetSingleWarehouseCompleted += DoGetSingleWarehouseCompleted;
            CurrentProduct = new Product();

            CanEditOrAdd = CurrentUser.CanAddOrderProduct || CurrentUser.CanEditOrderProduct;
            CanAdd = CurrentUser.CanAddOrderProduct;
        }

        void DoGetSingleWarehouseCompleted(object sender, SingleFeedbackCompletedEventArgs<Warehouse> e)
        {
            if (e.Error != null)
            {
                Message = e.Result.Message;
                MessageVisibility = Visibility.Visible;
            }
            else
            {
                Message = e.Result.Message;
                if (!e.Result.IsSuccessful)
                {
                    MessageVisibility = Visibility.Visible;
                }
                else
                {
                    MessageVisibility = Visibility.Collapsed;
                    ProductCollection = e.Result.Data.ProductCollection;
                    Count = ProductCollection.Count;
                    if (SavedPage.SavedCollectionProduct != null)
                    {
                        if (CollectionProduct.Count > 0 && CollectionProduct.Count <= Count)
                        {
                            ProductCollection = SavedPage.SavedCollectionProduct;
                        }

                    }

                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }


        #region Declare command
        //Save command use to save collection material order to database
        private DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new DelegateCommand<object>(DoSaveCommand);
                }
                return saveCommand;
            }
        }
         
        private DelegateCommand<object> cancelCommand;
        public DelegateCommand<object> CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new DelegateCommand<object>(DoBackCommand);
                }
                return cancelCommand;
            }
        }

        //Reset command use to reset list of material
        private DelegateCommand<object> resetCommand;
        public DelegateCommand<object> ResetCommand
        {
            get
            {
                if (resetCommand == null)
                {
                    resetCommand = new DelegateCommand<object>(DoResetCommand);
                }
                return resetCommand;
            }
        }

        #endregion

        #region implement command
        
        private void DoSaveCommand(object obj)
        {
            if (CurrentProduct.ProductID != 0 && TotalQuantity > 0 && TotalQuantity <= CurrentProduct.Quantity)
            {
                NewProduct = CurrentProduct;
                NewProduct.Quantity = TotalQuantity;
                NewProduct.PricePerUnit = PricePerUnit;
                CollectionProduct.Add(NewProduct);
                SavedPage.CollectionProduct = CollectionProduct;
                for (int i = 0; i < ProductCollection.Count; i++)
                {
                    if (ProductCollection[i].ProductID == NewProduct.ProductID)
                    {
                        ProductCollection.RemoveAt(i);
                    }
                }
                SavedPage.SavedCollectionProduct = ProductCollection;
                NavigationData ndProduct = new NavigationData();
                ndProduct.NavigationUri = WebConstants.NavigationPages.ProductExport;
                ndProduct.Data = SavedPage;

                NavigationManager.NavigateTo(WebConstants.NavigationPages.ExportProductOrder, ndProduct);
            }
            else
            {
                if (CurrentProduct.ProductID == 0)
                {
                    ErrorMessage = "Bạn chưa chọn sản phẩm";
                }
                else
                {
                    if (TotalQuantity > 0)
                    {
                        if (TotalQuantity > CurrentProduct.Quantity)
                        {
                            ErrorMessage = "Không đủ sản phẩm trong kho";

                        } 
                    }
                    else
                    {
                        ErrorMessage = "Bạn chưa điền số lượng sản phẩm";
                    }
                    
                }
            }

        }
        private void DoBackCommand(object obj)
        {
            NavigationData ndProduct = new NavigationData();
            ndProduct.NavigationUri = WebConstants.NavigationPages.ProductExport;
            ndProduct.Data = SavedPage;
            NavigationManager.NavigateTo(WebConstants.NavigationPages.ExportProductOrder, ndProduct);
        }
        private void DoResetCommand(object obj)
        {
            TotalQuantity = 0;
            PricePerUnit = 0;
        }

        #endregion

        #region biding data to combobox
        private ObservableCollection<Product> productCollection;
        public ObservableCollection<Product> ProductCollection
        {
            get { return productCollection; }
            set
            {
                if (!ReferenceEquals(productCollection, value))
                {
                    productCollection = value;
                    RaisePropertyChanged(() => ProductCollection);
                }
            }
        }
        #endregion
        #region Declare TotalQuantity

        private int totalQuantity;
        public int TotalQuantity
        {
            get { return totalQuantity; }
            set 
            { 
                totalQuantity = value;
                RaisePropertyChanged(()=>TotalQuantity);
            }
        }

        #endregion
        private int count;
        public int Count
        {
            get { return count; }
            set
            {
                count = value;
                RaisePropertyChanged(() => Count);

            }
        }
        private double pricePerUnit;
        public double PricePerUnit
        {
            get { return pricePerUnit; }
            set
            {
                pricePerUnit = value;
                RaisePropertyChanged(() => PricePerUnit);

            }
        }
        #region NewProduct

        private Product newProduct;
        public Product NewProduct
        {
            get { return newProduct; }
            set
            {
                if (!ReferenceEquals(newProduct, value))
                {
                    newProduct = value;
                    RaisePropertyChanged(() => NewProduct);
                }
            }
        }

        #endregion

        #region Declare Product

        private Product currentProduct;
        public Product CurrentProduct
        {
            get { return currentProduct; }
            set
            {
                if (!ReferenceEquals(currentProduct, value))
                {
                    currentProduct = value;
                    RaisePropertyChanged(() => CurrentProduct);
                }
            }
        }

        #endregion

        #region ErrorMessage

        private string errorMessage;
        public string ErrorMessage
        {
            get { return errorMessage; }
            set
            {
                if (!ReferenceEquals(errorMessage, value))
                {
                    errorMessage = value;
                    RaisePropertyChanged(() => ErrorMessage);

                }

            }
        }

        #endregion

        private ExportProductOrderViewModel savedPage;
        public ExportProductOrderViewModel SavedPage
        {
            get { return savedPage; }
            set
            {
                if (!ReferenceEquals(savedPage, value))
                {
                    savedPage = value;
                    RaisePropertyChanged(() => SavedPage);
                }
            }
        }
        #region CollectionProduct
        private ObservableCollection<Product> collectionProduct;
        public ObservableCollection<Product> CollectionProduct
        {
            get { return collectionProduct; }
            set
            {
                if (!ReferenceEquals(collectionProduct, value))
                {
                    collectionProduct = value;
                    RaisePropertyChanged(() => CollectionProduct);
                }
            }
        }
        #endregion
        #region Implementation of INavigationAware

        public override void OnNavigate(object extraData)
        {

            var nd = (NavigationData)extraData;
            if (nd != null)
            {
                if (nd.NavigationUri.Equals(WebConstants.NavigationPages.ExportProductOrder))
                {
                    if (nd.Data != null)
                    {
                        if (nd.Data.GetType().Equals(typeof(ExportProductOrderViewModel)))
                        {
                            SavedPage = (ExportProductOrderViewModel)nd.Data;
                            CollectionProduct = SavedPage.CollectionProduct;
                            ServiceCallCounter += ((ProductExportModel)Model).GetSingleWarehouseAsync(SavedPage.SelectedWarehouse.WarehouseID);
                           
                        }
                    }

                }
            }
            else
            {
                CollectionProduct = new ObservableCollection<Product>();
                SavedPage = new ExportProductOrderViewModel();
                
            }

        }

        public override void OnLeave()
        {
            //will be implemented in the sub classes
        }

        #endregion

    }
}
